package uz.rt.math;

/**
 * User: Wacko von Belsebub
 * Date: 10/30/11
 * Time: 11:31 PM
 */

/**
 * Class provides a set of static interpolations functions
 *
 * @author Wacko von Belsebub
 * @version 1.0.0
 */
public class Interpolator
{
    /**
     * Linear interpolation
     *
     * @param x  value for which f(x) is calculated
     * @param x0 first x parameter
     * @param x1 second x parameter
     * @param y0 first y parameter
     * @param y1 second y parameter
     * @return Linear interpolated value
     * @throws ArithmeticException thrown when x0==x1 and y0 != y1 because it make no mathematical sense
     */
    public static double linear(final double x, final double x0, final double x1, final double y0,
                                final double y1) throws ArithmeticException
    {
        if (x0 == x1)
        {
            if (y0 == y1) return y0;
            else throw new ArithmeticException("Linear interpolation: x0 must be different from x1");
        }
        return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
    }
}
